<?php

namespace iBrand\Store\Backend\Model;

use iBrand\Shop\Core\Models\O2oGoodsProducts;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use iBrand\Component\Goods\Models\GoodsProduct;

class Product extends GoodsProduct implements Transformable
{
	use TransformableTrait;

	public function getSpecStringAttribute()
	{
		$specStr = '';
		if ($this->attributes['spec_array']) {
			$specArr = json_decode($this->attributes['spec_array'], true);
			foreach ($specArr as $key => $val) {
				$specStr = $specStr . ',' . $val['value'];
			}
		}

		return ltrim($specStr, ",");
	}

	public function setSpecIDAttribute($value)
	{
		$this->attributes['specID'] = json_encode(explode('-', $value));
	}

	public function getSpecIDAttribute($value)
	{
		return json_decode($value);
	}

	public function scopeJudge($query, $sku, $id = 0)
	{
		return $query->where('sku', $sku)->where('id', '<>', $id)->get();
	}

    public function o2oProducts()
    {
        return $this->hasMany(O2oGoodsProducts::class, 'product_id');
    }

    public function getKeyCode($type = 'sku')
    {
        if ($type == 'spu') {
            return $this->goods_id;
        }

        return $this->sku;
    }

    public function getSeasonId()
    {
        return $this->goods->season_id;
    }
}
